﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DimensionalMeasurement
{
    public partial class FormWelcome : Form
    {

        public FormWelcome()
        {
            //完全启用双缓冲，降低界面绘制闪烁
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            InitializeComponent();
        }

        /// <summary>
        /// 更换图标
        /// </summary>
        public void iconChange()
        {
            try
            {
                if (PublicSet.Myset.picFileName != null && PublicSet.Myset.picFileName != "") pBIcon.Image = Image.FromFile(PublicSet.Myset.picFileName);
                if (PublicSet.Myset.iconFileName != null && PublicSet.Myset.iconFileName != "") Icon = new Icon(PublicSet.Myset.iconFileName);
            }
            catch (Exception)
            {
            }
        }

        private void FormWelcome_Load(object sender, EventArgs e)
        {
            PublicSet.LoadSet();//加载系统配置    
            iconChange();
            backgroundWorker.RunWorkerAsync(null);
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            backgroundWorker.ReportProgress(10, "Loading config.sys");
            Thread.Sleep(200);
            backgroundWorker.ReportProgress(20, "Loading MatrixFile.dll");
            Thread.Sleep(50);
            backgroundWorker.ReportProgress(30, "Loading OpenCV.dll");
            PublicSet.GetTuple();//加载矩阵
            Thread.Sleep(1000);
            backgroundWorker.ReportProgress(40, "Loading MvCameraControl.Net.dll");
            Thread.Sleep(30);
            backgroundWorker.ReportProgress(50, "Loading GSCameraAccess.dll");
            Thread.Sleep(30);
            backgroundWorker.ReportProgress(60, "Loading TSImageControl2.011.dll");
            Thread.Sleep(50);
            backgroundWorker.ReportProgress(70, "Loading MySql.Data.dll");
            Thread.Sleep(30);
            backgroundWorker.ReportProgress(85, "Loading Aruco130.dll");
            Thread.Sleep(50);
            backgroundWorker.ReportProgress(100, "Loading Camera");
            Thread.Sleep(20);
            //}

        }
        int width;
        private void FormWelcome_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(0, Height), Color.FromArgb(0, 168, 168), Color.FromArgb(2, 118, 118)), 0, 0, Width, Height);
            e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, 168, 168)), 0, 337, width, 23);
        }

        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            width = Width * e.ProgressPercentage / 100;
            lProgress.Text = e.UserState.ToString();
            Refresh();
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Tool.LogForm(e.Error);
            }
            Close();
        }
    }
}
